//
//  WHTWKWebViewVC.m
//  WZLProduct
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "WHTWKWebViewVC.h"
#import <WebKit/WebKit.h>

@interface WHTWKWebViewVC ()
@property (strong, nonatomic) WKWebView *webView;
@property (nonatomic, strong) UISlider *slider;
@end

@implementation WHTWKWebViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
    [self setNavigationBar];
    [self setWKWebView];
  
}
#pragma mark- 设置导航栏
- (void)setNavigationBar{
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HT_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)]];
    
 UIBarButtonItem *rightBarloveButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"hotlist1@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarloveButtonAction:)];
 UIBarButtonItem *rightBarshareButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HTshare@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarshareButtonAction:)];
    self.navigationItem.rightBarButtonItems = @[rightBarshareButton,rightBarloveButton];
    
}
#pragma mark 返回
- (void)back:(UIBarButtonItem *)sender{
    if (_webView.canGoBack) {
        [_webView goBack];
    }else{
       [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark 收藏
- (void)rightBarloveButtonAction:(UIBarButtonItem *)sender{
    NSLog(@"收藏");
}
#pragma mark 分享
- (void)rightBarshareButtonAction:(UIBarButtonItem *)sender{
      NSLog(@"分享");
}

#pragma mark - 设置网络视图
- (void)setWKWebView{
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 3)];
    self.slider.minimumTrackTintColor = [UIColor orangeColor];
    [self.slider setThumbImage:[UIImage imageNamed:@"HTThumbImage@2x"] forState:UIControlStateNormal];
    [self.view addSubview:self.slider];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.slider.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.slider.frame))];
    [self.view addSubview:_webView];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webHtmlUrl]]];

    //监听进度
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:0 context:nil];
    
}
/** 监听进度 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    NSLog(@"xxxxxxxxxxxxxxxx%f",_webView.estimatedProgress);
    self.slider.value = _webView.estimatedProgress;
    if (_webView.estimatedProgress > 0.6) {
        self.slider.value = 1;
    }
    
}
- (void)dealloc{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
}
@end
